###############################################                                     
# Cause of Effect? Turnout in Hispanic Majority-Minority Districts
#  - John A. Henderson, Jasjeet S. Sekhon, and Rocio Titiunik
#  - Forthcoming in Political Analysis
#  - Replication file for <Figure 2a>
#  - April 14, 2016
###############################################


options(width=150)
rm(list=ls())   

path='/local/'    
source(paste(path,'replicationPA/funs/headers.R',sep=''))

# Figure 2(a) Texas, 2000 Hispanic Registration
load(paste(path,'replicationPA/Figure2/TX_final_data_merged.Rdata',sep=''))

cd00=merged$y00.CD 
cd02=merged$y02.CD 
          
# define Hispanic districts for TX
h00=c(27,23,16,28,15,20) # in 2000
h02=c(27,23,16,28,15,20) # in 2002 

hisp.cd.2000=hisp.cd.2002=array(FALSE,nrow(merged))  
for(i in 1:length(h00)){
	ix1=which(cd00==h00[i])	
	ix2=which(cd02==h02[i])
	hisp.cd.2000[ix1]=TRUE 
	hisp.cd.2002[ix2]=TRUE		
}
rm(ix1,ix2)
                
# Any incumbents lose/retire/primaried in 2000; or retire/primaried in 2002; or other minority rep
r00=c(7,18,30)       
r02=c(5,25,26,31,18,30)
 
ix=which(cd00==7 | cd00==18 | cd00==30 | cd02==5 | 
	    cd02==25 | cd02==26 | cd02==31 | cd02==18 | cd02==30)    

# Define treatment  
Tr=array(NA,nrow(merged))
tr=which(hisp.cd.2000==T & hisp.cd.2002==F)
ct=which(hisp.cd.2000==F & hisp.cd.2002==F)  

Tr[tr]=TRUE
Tr[ct]=FALSE
Tr[ix]=NA
                
hispReg=merged$y00.registration.hisp
hispVAP=merged$pop.hispanic18    
totReg=merged$y00.registration 
phispVAP=merged$ppop.hispanic18            
   
Y=hispReg/hispVAP
Y[which(Y>1)]=NA
x=phispVAP
            
# Matching on P-HVAP 
# set matching seed if used             
set.seed(1005)                   
indxna=which(!is.na(Tr) & !is.na(x))
mout=Match(Tr=Tr[indxna],X=x[indxna],ties=F)

tr=Tr[indxna][c(mout$index.treated,mout$index.control)]  
y=Y[indxna][c(mout$index.treated,mout$index.control)]  
         
pdf(file=paste(path,'replicationPA/Figure2/conditionRegBias2000-TX.pdf',sep=''))
qqplot(y[!tr],y[tr],ylim=c(0,1),xlim=c(0,1),
	xlab="Blocks with same Non-Hispanic White incumbent before and after",
  	ylab="Blocks to be moved from Hispanic incumbent to Non-Hispanic White incumbent")
 	abline(a=0,b=1,col='red')   
	m1=mean(y[!tr],na.rm=T)
	m0=mean(y[tr],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
	rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()      
           
png(file=paste(path,'replicationPA/Figure2/conditionRegBias2000-TX.png',sep=''),res=65)
qqplot(y[!tr],y[tr],ylim=c(0,1),xlim=c(0,1),
	xlab="Blocks with same Non-Hispanic White incumbent before and after",
  	ylab="Blocks to be moved from Hispanic incumbent to Non-Hispanic White incumbent")
 	abline(a=0,b=1,col='red')   
	m1=mean(y[!tr],na.rm=T)
	m0=mean(y[tr],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
	rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()      

# End Figure2a.R
